﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Dynamic.Controls;

namespace SmartSpace.WpfShell.Modules.Dynamic.Controls
{
    /// <summary>
    /// Interaction logic for TimeControl.xaml
    /// </summary>
    public partial class TimeControl : UserControl, ISsControl
    {
        public string Time
        {
            get { return (string)GetValue(TimeProperty); }
            set { SetValue(TimeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Time.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TimeProperty =
            DependencyProperty.Register("Time", typeof(string), typeof(TimeControl), new PropertyMetadata(default(string), OnIdentityPropertyChangedCallback));

        static void OnIdentityPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TimeControl control = d as TimeControl;
            control._ctlList.SelectedItem = (string)e.NewValue;
        }

        public TimeControl()
        {
            InitializeComponent();

            for (int index = 0; index < 24; index++)
            {
                _ctlList.Items.Add(string.Format("{0:d2}:00", index));
            }
        }

        private void _ctlList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Time = _ctlList.SelectedItem.ToString();
        }

        #region ISsControl.IsReadOnly
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
            "IsReadOnly",
            typeof(bool),
            typeof(TimeControl),
            new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        private static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TimeControl control = d as TimeControl;
            bool isReadOnly = (bool)e.NewValue;
            control._ctlList.IsEnabled = !isReadOnly;
        }
        #endregion
    }
}
